package gunfire.relics;

import basemod.abstracts.CustomRelic;
import com.badlogic.gdx.graphics.Texture;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.PowerTip;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import gunfire.GunfireMod;
import gunfire.util.TextureLoader;

import java.util.ArrayList;

import static gunfire.GunfireMod.makeRelicOutlinePath;
import static gunfire.GunfireMod.makeRelicPath;

public class SafetyMeasures extends CustomRelic {

    public static final String ID = GunfireMod.makeID("SafetyMeasures");
    private static final Texture IMG = TextureLoader.getTexture(makeRelicPath("SafetyMeasures.png"));
    private static final Texture OUTLINE = TextureLoader.getTexture(makeRelicOutlinePath("blank.png"));
    private ArrayList<Integer> record;
    private int turn;

    public SafetyMeasures() {
        super(ID, IMG, OUTLINE, RelicTier.UNCOMMON, LandingSound.CLINK);
        this.counter = 0;
        this.record = new ArrayList<>();
        this.turn = 0;
    }

    @Override
    public void atBattleStart() {
        this.record = new ArrayList<>();
        this.turn = 0;
    }

    @Override
    public void atTurnStart() {
        if(this.turn == this.record.size()){
            this.record.add(0);
            this.setCounter();
        }
    }

    @Override
    public void onPlayerEndTurn() {
        this.turn++;
    }

    @Override
    public void wasHPLost(int damageAmount) {
        if (AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT && damageAmount > 0) {
            this.flash();
            if(this.turn == this.record.size()){
                this.record.add(damageAmount);
            } else {
                this.record.set(this.turn, this.record.get(this.turn) + damageAmount);
            }
            this.setCounter();
        }
    }

    @Override
    public void onVictory() {
        int heal;
        if(this.turn == this.record.size()){
            heal = this.record.get(this.turn - 1);
        } else {
            heal = this.record.get(this.turn);
        }
        if(heal > 0){
            AbstractDungeon.player.heal(heal);
            this.flash();
        }
        this.counter = 0;
        this.description = DESCRIPTIONS[0];
        this.tips.clear();
        this.tips.add(new PowerTip(this.name, this.description));
        this.initializeTips();
    }

    private void setCounter(){
        this.counter = this.record.get(this.turn);
        this.description = Settings.language == Settings.GameLanguage.ZHS
                ? "战斗结束后，回复 #b" + this.counter + " 生命。"
                : "At the end of combat, heal #b" + this.counter + " HP.";
        this.tips.clear();
        this.tips.add(new PowerTip(this.name, this.description));
        this.initializeTips();
    }

    @Override
    public String getUpdatedDescription() {
        return DESCRIPTIONS[0];
    }

    @Override
    public AbstractRelic makeCopy() {
        return new SafetyMeasures();
    }
}
